package com.example.wechat.csdnandroid.activity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.wechat.csdnandroid.R;
import com.example.wechat.csdnandroid.bean.Student;

public class ContentProviderAvtivity extends AppCompatActivity {
    private final static String AUTHORITY = "com.android.peter.provider";
    private final static Uri STUDENT_URI = Uri.parse("content://" + AUTHORITY + "/student");
    private static final String TAG = "ContentProviderAvtivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_provider_avtivity);
    }

    @SuppressLint("Recycle")
    public void query(View view) {
        Log.e(TAG, "query:11111 " );
        Cursor cursor = getContentResolver().query(STUDENT_URI, new String[]{"id", "name","gender","number","score"},null,null,null);
        assert cursor != null;
        while (cursor.moveToNext()) {
            Student student = new Student();
            student.id = cursor.getInt(cursor.getColumnIndex("id"));
            student.name = cursor.getString(cursor.getColumnIndex("name"));
            student.gender = cursor.getInt(cursor.getColumnIndex("gender"));
            student.number = cursor.getString(cursor.getColumnIndex("number"));
            student.score = cursor.getInt(cursor.getColumnIndex("score"));
            Log.d(TAG,"\nstudent = " + student.toString());
        }
        int count = cursor.getCount();
        //tab
        if (count==0) {
            Log.d(TAG,"\nstudent = null" );
        }
        Log.e(TAG, "query:22222 " );


    }

    public void insert(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "peter");
        contentValues.put("gender", 0);
        contentValues.put("number", "201804081705");
        contentValues.put("score", "100");
        getContentResolver().insert(STUDENT_URI, contentValues);
        ContentValues contentValues2 = new ContentValues();
        contentValues2.put("name", "gangdan");
        contentValues2.put("gender", 0);
        contentValues2.put("number", "201804081709");
        contentValues2.put("score", "100");
        getContentResolver().insert(STUDENT_URI, contentValues2);

    }


    public void updata(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","update");
        contentValues.put("gender",1);
        contentValues.put("number","201804111048");
        contentValues.put("score","90");

        getContentResolver().update(STUDENT_URI,contentValues,"number = ?",new String[] {"201804081709"});

    }

    public void delete(View view) {
        getContentResolver().delete(STUDENT_URI,"name = ?",new String[]{"update"});
    }
}
